package logic

import (
	"context"
	"strconv"
	"user-reg-mgr/model/mysql"

	"user-reg-mgr/goods/goods"
	"user-reg-mgr/goods/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type RegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLogic {
	return &RegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RegisterLogic) Register(in *goods.RegisterRequest) (*goods.RegisterResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User
	err := mysql.DB.Debug().Where("username=?", in.Username).First(&user)
	if err == nil {
		panic("用户已经存在")
	}
	data := mysql.User{
		UserName: in.Username,
		Password: in.Password,
	}
	if err := data.CreatUser(mysql.DB); err != nil {
		return nil, err
	}
	return &goods.RegisterResponse{
		UserId: strconv.Itoa(int(data.ID)),
	}, nil
}
